package com.cn.zpark;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.transform.Source;
import java.io.IOException;

/**
 * @PackageName: com.cn.zpark
 * @ClassName: SessionDemo04
 * @Description:
 * @author: RZS
 * @date: 2022/4/14  14:55
 */
@WebServlet("/sd4.do")
public class SessionDemo04 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 模拟用户退出登录

        // 获取session对象
        HttpSession session = request.getSession();
        // 设置session失效
        session.invalidate();

        // 执行以下代码报错： java.lang.IllegalStateException: getAttribute: 会话已失效
        String username = (String) session.getAttribute("username");
        System.out.println(username);
    }
}
